home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / ival / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-05-25  |  4.8 KB  |  183 lines  |  [TEXT/????]

  1. /*
  2.  * event.c - the main event loop
  3.  */
  4.  
  5. #include <quickdraw.h>
  6. #include <memory.h>
  7. #include <window.h>
  8. #include <dialog.h>
  9. #include <menu.h>
  10. #include <event.h>
  11. #include <desk.h>
  12. #include <control.h>
  13. #include <packages.h>
  14. #include <toolutil.h>
  15. #include <syserr.h>
  16.  
  17. #include "ival.h"
  18.  
  19. /*
  20.  * doevent() - one loop through the main event handler
  21.  *   Returns 1 if the user wants to continue; 0 if it's time to quit
  22.  */
  23.  
  24. int
  25. doevent()
  26. {
  27.     GrafPtr saveport;
  28.     EventRecord myevent;
  29.     char whichchar;        /* the char that was typed        */
  30.     WindowPtr whichwindow;    /* Points to window of MouseDown    */
  31.     WindowPtr activewindow;    /* Points to the frontmost window    */
  32.     DialogPtr whichdialog;    /* dialog of MouseDown            */
  33.     short whichitem;        /* mouse's dialog item            */
  34.     long menucommand;        /* a menu command to do (menu + item)    */
  35.     int windowcode;        /* What mouse was in when event posted. */
  36.     ControlHandle whichcontrol; /* the control that was pressed        */
  37.     short partcode;        /* the pressed part of that control    */
  38.     Point pt;            /* temp point                */
  39.     int userdone;        /* True when user wants to exit program    */
  40.     Point dlgorig;        /* topLeft of movable dialogs        */
  41.     Rect dragrect;        /* Bounds of window dragging        */
  42.  
  43.     userdone = 0;
  44.     SystemTask();        /* give the system a moment of time    */
  45.  
  46.     /*
  47.      * see which window is currently selected
  48.      * (for non-mouse events),
  49.      * Then grab an event and interpret it.
  50.      */
  51.  
  52.     GetPort(&activewindow);
  53.  
  54.     if (!GetNextEvent(everyEvent, &myevent)) {
  55.  
  56.     /*
  57.      * We're idle, so let's track the mouse (for placing notes).
  58.      */
  59.  
  60.     GetMouse(&pt);
  61.     notemouse(&pt);
  62.     } else if ((myevent.modifiers & cmdKey) &&
  63.       (myevent.what == keyDown || myevent.what == autoKey)) {
  64.  
  65.     /*
  66.      * It's a command-key keystroke.
  67.      * (command keystrokes are handled here since dialogs can't)
  68.      */
  69.  
  70.     whichchar = (char) (myevent.message & charCodeMask);
  71.     markmenus(activewindow);
  72.     menucommand = MenuKey(whichchar);
  73.     userdone = docommand(menucommand);
  74.     } else if (IsDialogEvent(&myevent)) {
  75.         /* here's where we'd handle modeless dialog events */
  76.     } else {
  77.     switch (myevent.what) {
  78.     case keyDown: 
  79.     case autoKey:
  80.         /* Here's where we'd handle character input */
  81.         break;
  82.     case updateEvt:        /* update the appropriate window */
  83.         if ((WindowPtr)(myevent.message) == windoc) {
  84.         redoc();
  85.         }
  86.         break;
  87.     case activateEvt:        /* activate or deactivate window */
  88.         if (myevent.modifiers & 1) {
  89.             SetPort((WindowPtr) myevent.message);
  90.         } else {
  91.             SetPort(screenport);
  92.         }
  93.         break;
  94.     case diskEvt:            /* disk inserted - eject or init it */
  95.         if (HiWord(myevent.message) != noErr) {
  96.         dlgorig.h = 90; dlgorig.v = 100;
  97.         (void) DIBadMount(pass(dlgorig), myevent.message);
  98.         }
  99.         break;
  100.     case mouseDown:            /* a mouse click someplace    */
  101.  
  102.         /*
  103.          * Remove the tracking note so that it doesn't trash the window.
  104.          */
  105.  
  106.         unnote();
  107.  
  108.         windowcode = FindWindow(pass(myevent.where), &whichwindow);
  109.         switch (windowcode) {
  110.         case inSysWindow:            /* a DA's window */
  111.         SystemClick(&myevent, whichwindow);
  112.         break;
  113.         case inMenuBar:            /* a menu item */
  114.         markmenus(activewindow);
  115.         menucommand = MenuSelect(pass(myevent.where));
  116.         userdone = docommand(menucommand);
  117.         break;
  118.         case inZoomIn:
  119.         case inZoomOut:            /* Zoom box    */
  120.         /* This program doesn't zoom or grow its window */
  121.         break;
  122.         case inGoAway:            /* Close box    */
  123.         if (TrackGoAway(whichwindow, pass(myevent.where))) {
  124.             if (whichwindow == windoc) {
  125.             userdone = 1;
  126.             }
  127.         }
  128.         break;
  129.         case inDrag:            /* Title Bar */
  130.         /* This program doesn't drag its window around */
  131.         break;
  132.         case inGrow:            /* Grow Region */
  133.             /* since we have no grow region, treat it as inContent */
  134.         case inContent:            /* Somewhere in the window */
  135.         if (whichwindow != FrontWindow()) {
  136.             /* the window isn't active - just activate it */
  137.             SelectWindow(whichwindow);
  138.         } else {
  139.  
  140.             /*
  141.              * The window is active,
  142.              *   interpret the mouse-down in this window's context.
  143.              */
  144.  
  145.             GlobalToLocal(&myevent.where);
  146.             if (whichwindow == windoc) {
  147.             if ((partcode = FindControl(pass(myevent.where),
  148.              whichwindow, &whichcontrol))) {
  149.  
  150.                 /*
  151.                  * We have only buttons, so control tracking
  152.                  * is easy.
  153.                  */
  154.  
  155.                 if (TrackControl(whichcontrol,
  156.                   pass(myevent.where), (ProcPtr) 0)) {
  157.                 if (PtInRect(pass(myevent.where),
  158.                   &cntlrect)) {
  159.                     cntlpress(whichcontrol);
  160.                 } else if (PtInRect(pass(myevent.where),
  161.                   &namerect)) {
  162.                     namepress(whichcontrol);
  163.                 } else if (PtInRect(pass(myevent.where),
  164.                   ¬erect)) {
  165.                     notepress(whichcontrol);
  166.                 }
  167.                 }
  168.             } else {
  169.                 if (PtInRect(pass(myevent.where), ¬erect)) {
  170.                 dropnote(&myevent.where);
  171.                 }
  172.             }
  173.             }
  174.  
  175.         }
  176.         break;
  177.         }
  178.         break;
  179.     }
  180.     }
  181.     return(!userdone);
  182. }
  183.